package com.insleton.admin.employee.model;

import com.vteba.common.model.AstModel;
import jakarta.persistence.*;

import java.util.Date;

@Entity
@Table(name = "employee") // 如果表名和entity相同（驼峰命名法），这个注解可以省略
@NamedQueries({
        @NamedQuery(name = "Employee.HQL", query = "select u from Employee u"),
        // 查询部分字段，如果hql这里不设置别名，TupleTransformer中aliases[]中，数组长度是对的，但是别名是null。但是也能正常转换
        @NamedQuery(name = "Employee.HQL.sub", query = "select u.id, u.state, u.deptId from Employee u"),
        // 去掉别名，查询也是OK的。因为有对应的构造函数。没有构造函数，也没有设置转换，那么将报错
        @NamedQuery(name = "Employee.HQL.sub.index", query = "select u.id, u.state, u.deptId from Employee u where u.name = ?1"),
        // 去掉别名，查询也是OK的
        @NamedQuery(name = "Employee.HQL.sub.query", query = "select u.id, u.state, u.deptId from Employee u where u.name = :name")
})
@NamedNativeQueries({
        // create query 不指定class，且这里也不设置resultclass，那么就可以查询任意字段了。不需要查询所有的字段了
        @NamedNativeQuery(name = "Employee.SQL.Entity.no.alias", query = "select id, name, state from employee"),
        @NamedNativeQuery(name = "Employee.SQL.Entity", query = "select u.id, u.name, u.state from employee u"),
        @NamedNativeQuery(name = "Employee.SQL.Entity.param", query = "select u.id, u.name, u.state from employee u where u.id = ?1"),
        @NamedNativeQuery(name = "Employee.SQL.Entity.named", query = "select u.id, u.name, u.state from employee u where u.id = :id"),
        // 不设置resultClass，会出现Named query exists, but did not specify a resultClass
        // 必须查询所有的栏位？好像是的
        // 别名要和返回类resultClass中属性名一样
        // 2024-09-11 , 去掉resultClass = SubEmp.class，在查询中指定就好了，使用转换器
        @NamedNativeQuery(name = "Employee.SQL", query = "select u.id, u.name, u.state from employee u"),
        // 去掉了别名 deptId
        // 2024-09-11 , 去掉了resultClass = EmpVo.class，在查询中指定就好了，使用转换器
        // 现在命名sql也可以和sql一样，可以查询任意字段了。hql也是可以的
        @NamedNativeQuery(name = "Employee.SQL.dto", query = "select id, name, state, dept_id from employee where id = :id"),
        @NamedNativeQuery(name = "Employee.SQL.dto.index", query = "select id, name, state, dept_id from employee where id = ?1")
})
@org.hibernate.annotations.NamedNativeQueries({
        // 去掉了 resultClass = EmpDTO.class，不需要在这里设置。使用转换器更方便。
        // 这里如果设置 resultClass = EmpDTO.class，EmpDTO必须是实体类，createQuery的时候，也要传递class
        @org.hibernate.annotations.NamedNativeQuery(
                name = "Employee.SQL.dto.h",
                query = "select id, name, state, dept_id from employee e where id = :id"
                )
})
public class Employee implements AstModel {
    @Id // 标注了id，建议依然标注@Column
    @Column
    private String id;
    @Column
    private String name;
    @Column(nullable = true)
    private String account;
    @Column(nullable = true)
    private String email;
    @Column(nullable = true)
    private Integer state;
    @Temporal(TemporalType.TIMESTAMP) // TIMESTAMP 包含日期和时间部分，DATE只有日期，TIME只有时间
    @Column(name = "entry_time")
    private Date entryTime;
    @Column(name = "dept_id", nullable = true)
    private String deptId;
    // private Integer pageNo;

    public Employee() {
    }

    // 如果在HQL中使用了构造函数，那这个构造函数不能少
    public Employee(String id, String name, Integer state) {
        this.id = id;
        this.name = name;
        this.state = state;
    }

    public Employee(String id, Integer state, String deptId) {
        this.id = id;
        this.state = state;
        this.deptId = deptId;
    }

    // public Employee(String id, String name, String account, String email) {
    //     this.id = id;
    //     this.name = name;
    //     this.account = account;
    //     this.email = email;
    // }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public Date getEntryTime() {
        return entryTime;
    }

    public void setEntryTime(Date entryTime) {
        this.entryTime = entryTime;
    }

    public String getDeptId() {
        return deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }
}
